package utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TaskMdlPathParse {

	public static List<String> getMdlRealPath(String taskFile){
		
		//获取本地接收到的task文件
		File temFile = new File(taskFile);
		if((!temFile.exists()) || temFile.isDirectory()){
			return null;
		}
		
		 //获取task文件
		 StringBuilder xmlStringBuilder = getXmlFileBuilder(temFile);
		 //获取XML文档对象
		 Document doc = stringToXml(xmlStringBuilder);
		 
		 List<String> mdlList = new ArrayList<String>();
		 Element element = doc.getDocumentElement();
		 NodeList nl = element.getElementsByTagName("file");
		
		 for(int i=0;i<nl.getLength();i++){
			 Node n = nl.item(i);
			 Element e = (Element)n;
			 NodeList temNl = e.getElementsByTagName("value");
			 String tmp = temNl.item(0).getChildNodes().item(0).getNodeValue();  
			 Log4jInit.logger.info("mdlList path:"+tmp);
			 mdlList.add(tmp);
		 }
		 
		 //转换路径，task中为O盘路径
		 //读取HDFS上的MDL文件
		 
		 //解析MDL文件
		 List<String> realPath = new ArrayList<String>();
		 for(String path:mdlList){
			 File f = new File(path);
			 realPath.add(parseMdlPath(f));
		 }
		 
		 List<String> hdfsPath = parseWinPathToHDFS(realPath);
		 
		 
		return null;
	}
	
	public static StringBuilder getXmlFileBuilder(File temFile){
		
		 StringBuilder xmlStringBuilder = new StringBuilder();
			
		 try {
	            BufferedReader br = new BufferedReader(new FileReader(temFile));
	            String temp = "";
	            while ((temp = br.readLine()) != null) {
	            	xmlStringBuilder.append(temp);
	            }
	            br.close();
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
		 return xmlStringBuilder;
	}
	
	
	public static Document stringToXml(StringBuilder xmlStringBuilder){
		
		 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		 Document doc = null;
		 
		 try {
			InputStream is = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("utf-8"));
			doc  = dbf.newDocumentBuilder().parse(is);
			is.close();
			
		} catch (SAXException | IOException | ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return doc;
	}
	
	/**
	 * 获取MDL文件中的数据真实路径，MDL与源数据一对一
	 * @param mdlFile
	 * @return
	 */
	public static String parseMdlPath(File mdlFile){
		
		if((!mdlFile.exists())||mdlFile.isDirectory()){
			return null;
		}
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			try {
				db = dbf.newDocumentBuilder();
				Document doc = db.parse(mdlFile);
				Element docEle = doc.getDocumentElement();
				
				 //<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 也是个节点，第二个节点为真实路径值
				String realPath = docEle.getFirstChild().getNextSibling().getTextContent(); 
				Log4jInit.logger.info("realPath::"+realPath);
				//list.add(realPath);
				return realPath;
				
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return null;
	}
	
	/**
	 * 将windows文件路径转换为HDFS路径
	 * @return
	 */
	public static List<String> parseWinPathToHDFS(List<String> winPath){
		
		List<String> parsePath = new ArrayList<String>();
		
		
		return null;
	}
		
	
	
	public static void main(String[] args){
		getMdlRealPath("/home/GF/sf/taskDemo.task");
	}
	
}
